import SignApi from "@/api/user/sign.js";
import Wx from "weixin-js-sdk";

/**
 * 微信 H5 签名
 */
const wxSign = (url) => {

	// 返回异步结果
	return new Promise((resolve, reject) => {

		// 发起签名
		SignApi.wechat(url).then(res => {
			let sign = res.obj;
			console.log("签名结果：", sign);

			// 执行微信签名
			Wx.config({
				debug: false,
				appId: sign.appId,
				timestamp: sign.timestamp,
				nonceStr: sign.noncestr,
				signature: sign.signature,
				jsApiList: [
					'checkJsApi',
					'onMenuShareAppMessage',
					'chooseWXPay',
					'chooseImage',
					'previewImage',
					'scanQRCode',
				]
			});

			// 签名结果
			Wx.ready(function(e) {
				console.log("[Success]微信环境准备:", e);
				resolve();
			});
			Wx.error(function(e) {
				console.log("[Error]微信环境准备:", e);
				reject(e);
			});
		}).catch(e => {
			console.log("[Error]微信环境准备:", e);
			reject(e);
		})
	});
};

export default {
	wxSign
}
